package book;

public class SynchronizedTest {

	static class MyObject{
		
		synchronized public void methodA(){
			
			System.out.println("begin method A：" + Thread.currentThread().getName());
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("end method A：" + Thread.currentThread().getName());
		}
		
//		synchronized 
		public void methodB(){
			
			System.out.println("begin method B：" + Thread.currentThread().getName());
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("end method B：" + Thread.currentThread().getName());
		}
	}
	
	static class ThreadA extends Thread{
		
		private MyObject object;
		
		public ThreadA(MyObject object) {
			
			this.object = object;
		}
		
		public void run(){
			
			object.methodA();
		}
	}
	
	static class ThreadB extends Thread{
		
		private MyObject object;
		
		public ThreadB(MyObject object) {
			
			this.object = object;
		}
		
		public void run(){
			
			object.methodB();
		}
	}
	
	public static void main(String[] args) {
		
		MyObject object = new MyObject();
		ThreadA ta = new ThreadA(object);
		ta.setName("A");
		ThreadB tb = new ThreadB(object);
		tb.setName("B");
		
		ta.start();
		tb.start();
	}
}
